/**
 * 初始化数据
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  self.$ySpliceAll(tiyData.tData.tabList);
  resetTabData(tiyData.tData.tab);
  tiyData.tParam.id = "";
  tiyData.tParam.name = "";
  tiyData.tParam.type = 0;
}

/**
 * 查询管理标签
 * @param self
 * @param tiyData
 */
export const listTabs = (self, tiyData) => {
  self.loadingData = true;
  self.$Spin.show();
  self.$ySpliceAll(tiyData.tData.tabList);

  let param = {
    "name": tiyData.tParam.name,
    "type": tiyData.tParam.type
  };

  self.$apiOther.queryManageTabs(param).then(function (response) {
    if(response.success){
      response.result.forEach(function(item){
        item.typeName = getTypeName(item.type);
        tiyData.tData.tabList.push(item);
      })
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
};

/**
 * 查询管理标签详情
 * @param self
 * @param tiyData
 */
export const getTabDetail = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOther.getManageTabDetail({"id": tiyData.tParam.id}).then(function(response){
    self.$Spin.hide();
    if(response.success){
      let tabData = response.result;
      tiyData.tData.tab.id = tabData.id;
      tiyData.tData.tab.name = tabData.name;
      tiyData.tData.tab.type = tabData.type;
      tiyData.tData.tab.description = tabData.description;
      tiyData.tData.tab.noSeo = tabData.noSeo;
      // tiyData.tData.tab.typeName =
      tiyData.tStatus.addModal.isShow = true;
    }else{
      self.$Message.error("查询标签详情失败！" + response.errorMessage);
    }
  })
}

/**
 * 保存管理标签详情
 * @param self
 * @param tiyData
 */
export const save = (self, tiyData) => {
  self.$refs['tabForm'].validate((valid) => {
    if (valid) {
      self.$Spin.show();
      self.$apiOther.saveManageTab(tiyData.tData.tab).then(function(response){
        self.$Spin.hide();
        if(response.success){
          listTabs(self, tiyData);
          self.$Message.success('保存成功');
          tiyData.tStatus.addModal.isShow=false;
        }else{
          self.$Message.error(response.errorMessage);
        }
      })
     }else{
       changeModelLoading(self, tiyData);
    }
  })
};

/**
 * 删除管理标签
 * @param self
 * @param tiyData
 */
export const deleteTab = (self,tiyData) => {
  self.$Spin.show();
  let param = {
    "id": tiyData.tParam.id,
    // "name":  tiyData.tParam.name
  }
  self.$apiOther.deleteManageTabDetail(param).then(function(response){
    self.$Spin.hide();
    if(response.success){
      self.$Message.success("删除成功");
      listTabs(self,tiyData);
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}

/**
 * 初始化tab数据
 * @param tab
 */
export const resetTabData = (tab) => {
  tab.id = "";
  tab.name = "";
  tab.type = 0;
  tab.typeName = "";
  tab.noSeo = false;
  tab.description = "";
  tab.createtime = "";
};

/**
 * 获取标签类型名称
 * @param type
 * @returns {string}
 */
function getTypeName(type){
  let typeName = "产品";
  switch (type) {
    case 1:
      typeName = "产品";
      break;
    case 2:
      typeName = "用户";
      break;
    case 3:
      typeName = "订单";
      break;
  }
  return typeName;
}

function changeModelLoading(self, tiyData) {
  tiyData.tStatus.addModal.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.addModal.isLoading = true;
  });
}
